package com.baoming.interceptor;

import java.util.ArrayList;
import java.util.List; 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
 
import com.baoming.util.LoginUtil;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

/**
 * 注册加强验证
 * 
 * @author Administrator
 * 
 */
@SuppressWarnings("serial")
public class RegistInterceptor extends AbstractInterceptor {

	/**
	 * 验证邮箱地址是否正确
	 * 
	 * @param email
	 * @return
	 *//*
	@Deprecated
	private boolean checkEmail(String email) {
		boolean flag = false;
		try {
			String check = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
			Pattern regex = Pattern.compile(check);
			Matcher matcher = regex.matcher(email);
			flag = matcher.matches();
		} catch (Exception e) {
			flag = false;
		}

		return flag;
	}

	@Deprecated
	private boolean checkLenth(String str, int max, int min) {
		if (str == null) {
			return false;
		} else if (str.length() <= max && str.length() >= min) {
			return true;
		} else {
			return false;
		}
	}

	@Deprecated
	private String RegistDeal(HttpServletRequest request) {

//		String name = request.getParameter("name").trim();
		String email = request.getParameter("email").trim();
//		String password = request.getParameter("password").trim();
//		String rePassword = request.getParameter("rePassword").trim();
		String code = request.getParameter("code").trim();
   
		
		 * 基本信息验证
		 
		if (LoginUtil.checkNumber(request, code)) {
			
			
			 * 查询用户是否存在
			 
			IMemberService memberService = (IMemberService) ServiceContainer
					.getService(IMemberService.SERVICE);
			if (memberService.isHas(email)) {
				request.setAttribute("regist_tip", "has");
				return "registError";
			}
			
			if (password != null && password.equals(rePassword)
					&& checkLenth(password, 20, 2)) {
				if (checkLenth(email, 20, 3) && checkEmail(email)) {
					if (checkLenth(name, 20, 2)) {  //名字最少为两个中文字符
						
						 * 查询用户是否存在
						 
						IMemberService memberService = (IMemberService) ServiceContainer
								.getService(IMemberService.SERVICE);
						if (memberService.isHas(email)) {
							request.setAttribute("regist_tip", "has");
							return "registError";
						}
						
						 * 注册成功
						 
						request.setAttribute("regist_tip", "success");
						return null;
					}
				}
			}
			
		}else{
			//验证码不正确
			request.setAttribute("regist_tip", "code");
			return "registError";
		}
		
		 * 注册不成功
		 
		request.setAttribute("regist_tip", "error");
		return "registError";
	}
*/
	@Override
	public String intercept(ActionInvocation invocation) throws Exception {

		HttpServletRequest request = ServletActionContext.getRequest();
		String suffer = request.getQueryString();

		/*
		 * struts2 漏洞处理
		 */
		if (suffer != null && suffer.contains("\\u0023")) {
			return "globalError";
		} else {
 
			/**
			 * 登陆处理,不让记录为空
			 */
			if(request.getRequestURI().contains("login_checkLogin")){
				String email = request.getParameter("email") ; 
				String password = request.getParameter("password") ; 
				if("".equals(email) || "".equals(password)){
					return "login" ; 
				} 
				HttpServletResponse response = ServletActionContext.getResponse() ; 
				String rem = request.getParameter("remember") ; 
				LoginUtil.remeberMeByCookie(request, response, email, password, rem) ; 
			}
			/**
			 * 用户不登陆处理
			 */
			else{ 
				String uri = request.getRequestURI() ; 
				
				//装入允许通过的界面 
				List<String> allowPage = new ArrayList<String>() ; 
				allowPage.add("d_") ; 
				allowPage.add("s_") ; 
				allowPage.add("article_detail") ; 
				allowPage.add("login_enter") ; 
				allowPage.add("execution") ; 
				allowPage.add("exe_upload") ; 
				allowPage.add("regist") ; 
				allowPage.add("prop_optSearch") ; 
				
				//默认设置为不允许通过
				boolean isAllowPage = false ; 
				
				//URI是否在允许通过的链接 
				for(String allowUri : allowPage){
					if(uri.contains(allowUri)){
						
						//在允许通过的链接
						isAllowPage = true ; 
						break ; 
					}
				}
				
				//不在允许通过的链接 进入Session验证
				if(!isAllowPage){
					
					//验证用户Session 为空 直接返回登陆界面 
					if(request.getSession().getAttribute("global_member") == null){
						
						//消除所有Session
						request.getSession().invalidate() ; 
						
						//返回登陆界面 
						return "login" ; 
					}
				}
			}

			return invocation.invoke();
		}
	}
 
}
